﻿//-----------------------------------------------------------------------------------------------------------
// WBFSSync Project by Omega Frost 
// http://wbfssync.codeplex.com/
//
// WBFSSync is Licensed under the terms of the 
// Microsoft Reciprocal License (Ms-RL)
//-----------------------------------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using WBFSe3.Wbfs;
using WBFSe3.IO;
using System.Windows;

namespace WbfsSync.Core
{
    //-------------------------------------------------------------------------------------------------------
    //
    //-------------------------------------------------------------------------------------------------------
    public static class Config
    {
        //---------------------------------------------------------------------------------------------------
        //
        //---------------------------------------------------------------------------------------------------
        public const String Path = "Config\\";
        public const String ConfigFile = "config.ini";
        public const String FatWbfsFile = "fatwbfs.ini";
        public const String LibrariesFile = "libraries.ini";


        //---------------------------------------------------------------------------------------------------
        //
        //---------------------------------------------------------------------------------------------------

        // Browser
        public static BrowserMode BrowserMode = Core.BrowserMode.Tile;
        public static Int32[] IconSizes = new Int32[] { 72, 16, 16, 48, 96, 256 };
        public static Int32[] ItemWidths = new Int32[] { 268, 0, 238, 84, 114, 278 };
        
        // Libraries
        public static List<String> Libraries = 
            new List<String>();

        // Copy
        public static String DefaultPcExt = ".iso";
        public static String DefaultFatWbfsExt = ".wbfs";

        // Icons
        public static bool DisableFolderIconLoading = true;
        public static bool DisableFileIconLoading = true;

        // Cover
        public static CoverMode CoverMode = CoverMode.DiscCustom;

        //---------------------------------------------------------------------------------------------------
        //
        //---------------------------------------------------------------------------------------------------
        public static void Load()
        {
            try
            {
                if (File.Exists(Path + ConfigFile))
                {
                    String[] config = File.ReadAllLines(Path + ConfigFile);
                    foreach (String line in config)
                    {
                        if (line.StartsWith("WindowPosX="))
                        {
                            int v = int.Parse(line.Substring(11));
                            Application.Current.Windows[0].Left = v;
                        }
                        else if (line.StartsWith("WindowPosY="))
                        {
                            int v = int.Parse(line.Substring(11));
                            Application.Current.Windows[0].Top = v;
                        }
                        else if (line.StartsWith("WindowWidth="))
                        {
                            int v = int.Parse(line.Substring(12));
                            if (v > 0)
                            {
                                Application.Current.Windows[0].WindowState = WindowState.Normal;
                                Application.Current.Windows[0].Width = v;
                            }
                            else
                            {
                                Application.Current.Windows[0].WindowState = WindowState.Maximized;
                                Application.Current.Windows[0].Width = -v;
                            }
                        }
                        else if (line.StartsWith("WindowHeight="))
                        {
                            int v = int.Parse(line.Substring(13));
                            if (v > 0)
                            {
                                Application.Current.Windows[0].WindowState = WindowState.Normal;
                                Application.Current.Windows[0].Height = v;
                            }
                            else
                            {
                                Application.Current.Windows[0].WindowState = WindowState.Maximized;
                                Application.Current.Windows[0].Height = -v;
                            }
                        }
                        else if (line.StartsWith("BrowserMode="))
                        {
                            BrowserMode = (BrowserMode)int.Parse(line.Substring(12));
                        }
                        else if (line.StartsWith("DefaultPcExt="))
                        {
                            DefaultPcExt = line.Substring(13);
                        }
                        else if (line.StartsWith("DefaultFatWbfsExt="))
                        {
                            DefaultPcExt = line.Substring(18);
                        }
                        else if (line.StartsWith("DisableFolderIconLoading="))
                        {
                            int v = int.Parse(line.Substring(25));
                            if (v == 0) DisableFolderIconLoading = false;
                        }
                        else if (line.StartsWith("DisableFileIconLoading="))
                        {
                            int v = int.Parse(line.Substring(23));
                            if (v == 0) DisableFileIconLoading = false;
                        }
                        else if (line.StartsWith("CoverMode="))
                        {
                            int v = int.Parse(line.Substring(10));
                            CoverMode = (CoverMode)v;
                        }
                    }
                }
            }
            catch
            {
                MessageBox.Show("Failed to load the configuration file", 
                    "WbfsSync", MessageBoxButton.OK, MessageBoxImage.Error);
            }

            try
            {
                if (File.Exists(Path + FatWbfsFile))
                {
                    String[] serials = File.ReadAllLines(Path + FatWbfsFile);
                    foreach (String serial in serials)
                    {
                        uint sn;
                        if (uint.TryParse(serial, out sn))
                            WbfsDriveInfo.AddFatWbfsSN(sn);
                    }
                }
            }
            catch
            {
                MessageBox.Show("Failed to load the fatwbfs file",
                    "WbfsSync", MessageBoxButton.OK, MessageBoxImage.Error);
            }

            try
            {
                if (File.Exists(Path + LibrariesFile))
                {
                    String[] libs = File.ReadAllLines(Path + LibrariesFile);
                    for(int i = 0; i < libs.Length; i++)
                    {
                        String lib = libs[i];

                        if (!lib.EndsWith("\\"))
                            lib += "\\";

                        if (Directory.Exists(lib))
                            Libraries.Add(lib);
                    }
                }
            }
            catch
            {
                MessageBox.Show("Failed to load the libraries file",
                    "WbfsSync", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }

        //---------------------------------------------------------------------------------------------------
        //
        //---------------------------------------------------------------------------------------------------
        public static void Save()
        {
            try
            {
                if (!Directory.Exists(Path))
                    Directory.CreateDirectory(Path);

                String[] lines = new String[10];

                lines[0] = "WindowPosX=" + (int)Application.Current.Windows[0].Left;
                lines[1] = "WindowPosY=" + (int)Application.Current.Windows[0].Top;
                
                lines[2] = "WindowWidth=" + ((Application.Current.Windows[0].WindowState == WindowState.Maximized) ?
                    -(int)Application.Current.Windows[0].Width : (int)Application.Current.Windows[0].Width);
                lines[3] = "WindowHeight=" + ((Application.Current.Windows[0].WindowState == WindowState.Maximized) ?
                    -(int)Application.Current.Windows[0].Height : (int)Application.Current.Windows[0].Height);

                lines[4] = "BrowserMode=" + (int)BrowserMode;

                lines[5] = "DefaultPcExt=" + DefaultPcExt;
                lines[6] = "DefaultFatWbfsExt=" + DefaultFatWbfsExt;

                lines[7] = "DisableFolderIconLoading=" + (DisableFolderIconLoading ? 1 : 0);
                lines[8] = "DisableFileIconLoading=" + (DisableFileIconLoading ? 1 : 0);

                lines[9] = "CoverMode=" + (int)CoverMode;

                File.WriteAllLines(Path + ConfigFile, lines);
            }
            catch
            {
                MessageBox.Show("Failed to save the configuration file",
                    "WbfsSync", MessageBoxButton.OK, MessageBoxImage.Error);
            }

            try
            {
                if (!Directory.Exists(Path))
                    Directory.CreateDirectory(Path);

                uint[] sns = WbfsDriveInfo.GetFatWbfsSNs();
                var lines = from s in sns select s.ToString();
                File.WriteAllLines(Path + FatWbfsFile, lines.ToArray());
            }
            catch
            {
                MessageBox.Show("Failed to save the fatwbfs file",
                    "WbfsSync", MessageBoxButton.OK, MessageBoxImage.Error);
            }

            try
            {
                if (!Directory.Exists(Path))
                    Directory.CreateDirectory(Path);

                File.WriteAllLines(Path + LibrariesFile, Libraries.ToArray());
            }
            catch
            {
                MessageBox.Show("Failed to save the libraries file",
                    "WbfsSync", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }
    }
}
